import logging
import logging.handlers

from cloghandler import ConcurrentRotatingFileHandler
from flask import g

from utils.encode import uuid

try:
    from config import LOG_LEVEL, LOG_FILE, HB_LOG_FILE, TASK_LOG, SQL_LOG
except ImportError:
    LOG_LEVEL, LOG_FILE, HB_LOG_FILE, TASK_LOG, SQL_LOG = logging.INFO, '/log/web/view.log', '/log/web/hb-view.log', '/log/web/task.log', '/log/web/sql.log'


class RequestIdFilter(logging.Filter):

    def filter(self, record):
        g.request_id = getattr(g, 'request_id', None) or uuid()
        record.request_id = g.request_id
        return True


def init_logger_config():
    logger = logging.getLogger('ugc')
    logger.setLevel(LOG_LEVEL)
    formatter = logging.Formatter(
        '[%(asctime)s] [%(levelname)s] [%(name)s]:[%(filename)s]:[%(funcName)-6s]:[%(request_id)s] %(message)s')
    logger.addFilter(RequestIdFilter())
    if LOG_FILE:
        rotateHandler = ConcurrentRotatingFileHandler(
            LOG_FILE, "a", 100 * 1024 * 1024, 60)
        rotateHandler.setFormatter(formatter)
        logger.addHandler(rotateHandler)
    else:
        ch = logging.StreamHandler()
        ch.setFormatter(formatter)
        logger.addHandler(ch)

    ####################################################################################################################

    logger = logging.getLogger('ugc_heartbeat')
    logger.setLevel(LOG_LEVEL)
    formatter = logging.Formatter(
        '[%(asctime)s] [%(levelname)s] [%(name)s]:[%(filename)s]:[%(funcName)-6s] %(message)s')
    if HB_LOG_FILE:
        rotateHandler = ConcurrentRotatingFileHandler(
            HB_LOG_FILE, "a", 200 * 1024 * 1024, 60)
        rotateHandler.setFormatter(formatter)
        logger.addHandler(rotateHandler)
    else:
        ch = logging.StreamHandler()
        ch.setFormatter(formatter)
        logger.addHandler(ch)

    ####################################################################################################################

    logger = logging.getLogger('ugc_task')
    logger.setLevel(LOG_LEVEL)
    formatter = logging.Formatter(
        '[%(asctime)s] [%(levelname)s] [%(name)s]:[%(filename)s]:[%(funcName)-6s] %(message)s')
    if TASK_LOG:
        fh = logging.handlers.TimedRotatingFileHandler(
            TASK_LOG, "midnight", backupCount=40)
        fh.setFormatter(formatter)
        logger.addHandler(fh)
    else:
        ch = logging.StreamHandler()
        logger.addHandler(ch)

        ch.setFormatter(formatter)

    ####################################################################################################################

    logger = logging.getLogger('ugc_sql')
    logger.setLevel(LOG_LEVEL)
    formatter = logging.Formatter(
        '[%(asctime)s] [%(levelname)s] [%(name)s]:[%(filename)s]:[%(funcName)-6s] %(message)s')
    if SQL_LOG:
        rotateHandler = ConcurrentRotatingFileHandler(
            SQL_LOG, "a", 100 * 1024 * 1024, 20)
        rotateHandler.setFormatter(formatter)
        logger.addHandler(rotateHandler)
    else:
        ch = logging.StreamHandler()
        logger.addHandler(ch)


def set_logger_config():
    logger = logging.getLogger('ugc')
    setattr(g, 'log', logger)


def set_heartbeat_config():
    logger = logging.getLogger('ugc_heartbeat')
    setattr(g, 'log', logger)


def set_task_config():
    logger = logging.getLogger('ugc_task')
    setattr(g, 'log', logger)


def get_log():
    log = getattr(g, 'log', None)
    if not log:
        set_logger_config()
        log = getattr(g, 'log', None)
    return log


def get_sql_log():
    return logging.getLogger('ugc_sql')


def get_task_log():
    return logging.getLogger('ugc_task')


init_logger_config()
